<template>
  <div class="ts-login" ref="fullScreen">
    <div class="ts-logo">
      <svg class="ts-logo-svg" id="组_2" data-name="组 2" xmlns="http://www.w3.org/2000/svg"
           xmlns:xlink="http://www.w3.org/1999/xlink"
           width="127" height="127" viewBox="0 0 127 127">
        <metadata><?xpacket begin="﻿" id="W5M0MpCehiHzreSzNTczkc9d"?>
          <x:xmpmeta xmlns:x="adobe:ns:meta/"
                     x:xmptk="Adobe XMP Core 5.6-c138 79.159824, 2016/09/14-01:09:01        ">
            <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
              <rdf:Description rdf:about=""/>
            </rdf:RDF>
          </x:xmpmeta>
          <?xpacket end="w"?></metadata>
        <image id="矢量智能对象" width="127" height="127"
               xlink:href="data:img/png;base64,iVBORw0KGgoAAAANSUhEUgAAAH8AAAB/CAMAAADxY+0hAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAB4FBMVEUdnf////8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf8dnf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+/fA/n2wPw72H2D9co5d/40cW/9KwP5DsN8C9gNYv5VjfRPYn8D6wUmsP8Wx/iOwP4D1SznWf4z1/QasX8+h/vQTrG/nWH8c63s51n+K9Wc41K+exfohDvXLDFh/jkN9D4f6vmdQX/+cQ/0ODvVHOdSfMkP9F/Mc6Y+9ZEg/zCP9V68Z0h+KDtvA/DfzDOl7zAzo69/GGw0h/rv3DOl/+MiAfl9FcpOzaL/3k5nh/OnP2pQOf6H+F/l8mzhf3/+VMZi/EAowhvL9MxPfGMP4099DxnaWMar6iz9+On+xrFHJX6z6/WdOyTRtqu+T8k5sKjMoLfx1OTqLh61t7kkFclr/H3z0Rw101nvAAAAAAElFTkSuQmCC"></image>
        <image id="矢量智能对象-2" data-name="矢量智能对象" x="75" y="38" width="22" height="20"
               xlink:href="data:img/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAUCAQAAAAjdQW7AAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAmJLR0QAAKqNIzIAAAAJcEhZcwAACxIAAAsSAdLdfvwAAAAHdElNRQfhCA8OLRxHahK6AAAA80lEQVQoz42SO0pDURRFV16iRVpRsLNxBgGxFgtRLEVrCzMDxyCkVCvBJp2KMZU2IkjIHCSQ5oGfUgtBzFsWSTTR+O7d5T3rbs7ZbABM3LXtu5+mnrkK4KZNn8x8885thrLsteO69ObXy7nTfbhujI4Al6NQ1UrCHrGqFuyyEAl3sadq1avgGr0EAZjnNuicYarqg3O+BJzThBYAi+yzwmuu8z3ufP+s+5HrvIUlO1Epdyz1WxCj9WE7joPo4U+Vpmzmog2LI4datvEvejFo3AhetDYRPbAwMUg3fB4DH13Lyd1ZTwdg5okzwQq4ZNuWlb+TL44ICLyPVnmyAAAAAElFTkSuQmCC"></image>
        <image id="矢量智能对象-3" data-name="矢量智能对象" x="30" y="32" width="65" height="76"
               xlink:href="data:img/png;base64,iVBORw0KGgoAAAANS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Jppgy0ul1znycD8KIDmJtWdaEmggDAdT/A3ECANl9ELAivGTN7Zj2XbBM+GCarOunG7pnqwoL8thMnI1AKHcfpWYD5gfC3M8ppKlTx8ezi7xcWtvMuQvgWfw/hPMWe4rArAKM85fwVh1MrZmfYAX6y0MlXoMX2WVgBGpjm7ClwkaqCvWFx38VzS/uRFt4U1nLMyH0fqwMv8Jr8ZzMfciIk9628zCTJvjYNy2YNPwXhvJt3uY+WxrYVVFHRg0LsQR48SEfyotLHIAbwG+14j1doVFTypuQfKmc0527dCnYAAAAASUVORK5CYII="></image>
      </svg>
    </div>
    <ts-input id="phone" tabindex="1" v-model="tel" label="账号" placeholder="请输入您的手机号" type="tel"></ts-input>
    <ts-input tabindex="2" v-model="password" label="密码" placeholder="请输入您的登录密码"
              type="password"></ts-input>
    <div class="submit-btn">
      <ts-button type="primary" @click="login()" size="large">登录</ts-button>
    </div>
    <div class="other-action">
      <div class="action-link">
        <router-link to="/forgot">忘记密码</router-link>
        <!--忘记密码-->
      </div>
      <div class="action-link">
        <router-link to="/register">注册</router-link>
        <!--注册-->
      </div>
    </div>
  </div>
</template>

<script>
  import verify from '../../utils/verify'
  import {SET_USER, SET_REGISTER, REMOVE_REGISTER, SET_ISFIRST} from '../../store/mutations-types'
  import {LOGIN, POST_UPLOCATION, EDIT_WORK_STATUS, VERSION} from '../../assets/js/api'
  import store from '../../store'
  import transfer from '../../core/transfer'

  export default {
//    beforeRouteEnter(to, from, next) {
//      let {type} = store.state.registerRecord;
//      console.log(type)
//      if (type === 1) {
//        next('/register/addProfile')
//      }
//      if (type === 2) {
//        next('/register/authInfo')
//      }
//      if (type === 3) {
//        next('/register/pending')
//      }
//      next()
//    },
    name: 'login',
    data() {
      return {
        tel: '',
        password: ''
      }
    },
    async created() {
      if ($prod) {
        const string = device.platform;
        const oldVersion = AppVersion.version;
        const json = await this.$http.get(VERSION, null, {show: true, type: 'mini'});
        if (json.result === 1) {
          const newVersion = string === 'Android' ? json.data.store_android_beta : json.data.store_ios_beta;
          const url = string === 'Android' ? json.data.store_android_url : json.data.store_ios_url;
          const content = json.data.market_content;
          if (oldVersion !== newVersion && json.data.store_update_force === 1) {
            this.$modal({message: `为了不影响您的正常使用,请下载最新版本，更新内容: ${content}`, cancel: true,lock: true})
              .then(action => {
                window.location.assign(url)
                this.$toast('正在下载中,请稍后再用');
                navigator.app.exitApp();
              },()=>{
                navigator.app.exitApp();
              })
          }
        }
      }
    },
    methods: {
      validation() {
        if (!verify.requiredObjProp(this.$data)) return {valid: false, text: '有未填写项，请检查'}
        if (!verify.mobile(this.tel)) return {valid: false, text: '手机格式错误'};
        if (!verify.password(this.password)) return {valid: false, text: '密码格式错误'}
        return {valid: true}
      },
      async login() {
        let res = this.validation();
        if (res.valid) {
          let json = await this.$http.post(LOGIN, this.$data, {show: 'true', text: '登录中...'});
          if (json.result === 1 && json.data.submit_to === 3) {
            let {is_status, id, token} = json.data;
            if (is_status === 2) {
              if (!$prod) {
              } else {
                //极光推送设置别名
                await window.JPush.setAlias({sequence: 1, alias: id},
                  (result) => {
                    let sequence = result.sequence;
                    let alias = result.alias;
                    console.log(sequence + '---' + alias);
                  }, (error) => {
                    let sequence = error.sequence;
                    let errorCode = error.code;
//                      alert(sequence + '---' + errorCode)
                  })
                baidumap_location.getCurrentPosition(result => {
                  const lat = result.latitude;
                  const lng = result.longitude;
                  this.$http.post(POST_UPLOCATION, {lat, lng});
                }, error => {
                  this.$toast('定位失败！请联系客服');
                });

              }
              let {tel, name, member_number, image} = json.data;
              let currObj = {member_id: id, token, tel, name, member_number, image};
              this.$store.commit(SET_USER, currObj);
              this.$store.commit(REMOVE_REGISTER);
              const isFirst = this.$store.state.isFirst;
              let toast = this.$toast({message: '登录成功,跳转中...', position: 'top'});
              this.$http.post(EDIT_WORK_STATUS, {status: 1});
              //如果为安装APP后第一次登录
              if (isFirst === 'true') {
                //设置为非第一次
                this.$store.commit(SET_ISFIRST, false);
                //跳转到引导页
                setTimeout(() => {
                  toast.close();
                  this.$router.replace('/guide');
                }, 1500);
              } else {
                let routeName = this.$route.query.beforeRoute;
//                if (routeName) {
//                  transfer.$emit('loginBack');
//                }
                let beforeRoute = routeName || '/index';
                setTimeout(() => {
                  toast.close();
                  this.$router.replace(beforeRoute);
                }, 1500);
              }
            } else {
              let tel = this.tel;
              this.$store.commit(SET_REGISTER, {member_id: id, token: token, tel: tel, type: 3});
              this.$router.replace({path: 'register/pending'});
            }
          } else if (json.result === 1 && json.data.submit_to !== 3) {
            let {submit_to, token, id, tel} = json.data;
            this.$store.commit(SET_REGISTER, {member_id: id, token: token, tel: tel, type: submit_to});
            if (json.data.submit_to === 1) {
              this.$router.push({path: 'register/addProfile'})
            } else if (json.data.submit_to === 2) {
              this.$router.push({path: 'register/authInfo'})
            }
          }
        } else {
          this.$toast({
            message: res.text,
            position: 'bottom'
          });
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  @import "../../assets/css/mixin";

  .ts-login {
    min-height: 100vh;
    background-color: #ffffff;
    .ts-logo {
      text-align: center;
      padding: 1.15rem 0 .96rem;
      .ts-logo-svg {
        @include wh(1.27rem, 1.27rem);
      }
    }
    .submit-btn {
      padding: 0 .35rem;
      margin-top: .78rem;
    }

    .other-action {
      padding: .18rem .35rem;
      display: flex;
      .action-link {
        flex: 1;
        a {
          @include sc(.2601rem, #2E2E2E);
        }
        &:last-child {
          text-align: right;
        }
      }
    }
  }
</style>
